package com.ecust.ml.cardriving.view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;

import com.ecust.ml.cardriving.controller.CarDriverController;
import com.ecust.ml.cardriving.model.Car;
import com.ecust.ml.cardriving.model.Highway;
import com.ecust.ml.cardriving.model.Road;

public class HighwayViewer extends JPanel{

	private static final long serialVersionUID = -8562321061329334586L;
	
	public static final int HIGHWAYVIEWER_WIDTH = CarDrivingSimulator.WIDTH / 2;
	public static final int ROAD_VIEW_WIDTH = 20;
	
	public static boolean VIEW_OF_FOLLOW = true;	// 是否跟踪视角
	
	private Highway highway;
	
	private Car targetCar;
	
	/**
	 * 视角
	 */
	private String viewMode;
	
	public HighwayViewer(Highway highway, Car targetCar, String viewMode) {
		this.highway = highway;
		this.targetCar = targetCar;
		this.viewMode = viewMode;
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.clearRect(0, 0, HIGHWAYVIEWER_WIDTH, CarDrivingSimulator.HEIGHT);
		
		// 根据目标小车所在车道，对视角进行偏移
		int offset = -(targetCar.getLaneOn() - Highway.LANE_MIDDLE) * ROAD_VIEW_WIDTH;
		if(!VIEW_OF_FOLLOW) {
			offset = 0;
		}
		
		// 道路及小车
		Iterator<Road> roads = this.highway.getRoads().values().iterator();
		while(roads.hasNext()) {
			Road road = roads.next();
			this.renderRoad(g, road, offset);
		}
		
		// 目标小车
		g.setColor(Color.blue);
		this.renderACar(g, targetCar, offset);
		
		// 路两侧
		g.setColor(Color.green);
		int otherWidth = (HIGHWAYVIEWER_WIDTH - this.highway.getRoads().size() * 
				ROAD_VIEW_WIDTH)/2;
		g.fillRect(0, 0, otherWidth+offset, CarDrivingSimulator.HEIGHT);
		g.fillRect(HIGHWAYVIEWER_WIDTH - otherWidth+offset, 0, otherWidth-offset, CarDrivingSimulator.HEIGHT);
		
		// 其他信息
		renderInfo(g);
		
		g.setColor(Color.black);
	}
	
	/**
	 * 渲染道路
	 * @param g
	 * @param road
	 */
	private void renderRoad(Graphics g, Road road, int offset) {
		
		// 渲染道路两侧
		int x = this.getXByLaneOn(road.getIndex()) - ROAD_VIEW_WIDTH/2 + offset;
		if(road.getIndex() == 1 || road.getIndex() == 5) {
			// 路肩特殊处理
			g.setColor(new Color(160, 107, 15));
			g.fillRect(x, 0, ROAD_VIEW_WIDTH, CarDrivingSimulator.HEIGHT);
			g.setColor(Color.black);
		} else {
			g.drawLine(x, 0, x, CarDrivingSimulator.HEIGHT);
			g.drawLine(x+ROAD_VIEW_WIDTH, 0, x+ROAD_VIEW_WIDTH, CarDrivingSimulator.HEIGHT);
			
			// 划分道路距离特征值
			/*
			for(int i=0; i< CarDrivingSimulator.HEIGHT; i++) {
				g.drawLine(x, i*50, x+ROAD_VIEW_WIDTH, i*50);
			}
			*/
		}
		
		// 渲染道路上小车
		g.setColor(Color.red);
		List<Car> cars = road.getCars();
		for(int i=0;i<cars.size();i++) { 
			this.renderACar(g, cars.get(i), offset);
		}
		g.setColor(Color.black);
	}
	
	/**
	 * 渲染小车
	 * @param g
	 * @param car
	 * @param offset 偏移值
	 */
	private void renderACar(Graphics g, Car car, int offset) {
		int laneOn = car.getLaneOn();
		int position = car.getPosition();
		g.fillRect(this.getXByLaneOn(laneOn) - Car.CAR_WIDTH/2 + offset, CarDrivingSimulator.HEIGHT - position, Car.CAR_WIDTH, car.getLength());
	}
	
	/**
	 * 由所在车道获取渲染的x坐标值
	 * @param laneOn
	 * @return
	 */
	private int getXByLaneOn(int laneOn) {
		int x = HIGHWAYVIEWER_WIDTH/2 + (laneOn - Highway.LANE_MIDDLE) * ROAD_VIEW_WIDTH;
		return  x;
	}
	
	/**
	 * 渲染冲突
	 * @param g
	 */
	private void renderCollision(Graphics g) {
		g.setColor(Color.red);
		g.drawString("碰撞", 10, 100);
		g.setColor(Color.black);
	}
	
	private void renderInfo(Graphics g) {
		if(CarDriverController.collision) {
			renderCollision(g);
		}
		this.renderMode(g);
		this.renderSpeed(g);
		this.renderView(g);
	}
	
	/**
	 * 渲染速度
	 * @param g
	 */
	private void renderSpeed(Graphics g) {
		g.setColor(Color.black);
		g.drawString("速度:"+targetCar.getSpeed()+"km/h", 10, 20);
	}
	
	/**
	 * 渲染视角
	 * @param g
	 */
	private void renderView(Graphics g) {
		g.setColor(Color.black);
		g.drawString("视角:"+viewMode, 10, 40);
	}
	
	/**
	 * 渲染模式
	 * @param g
	 */
	private void renderMode(Graphics g) {
		g.setColor(Color.black);
		g.drawString("模式:"+(CarDriverController.autoMode?"自动":"手动"), 10, 60);
		g.drawString("数据:"+(CarDriverController.trainMode?"采集中":"未采集"), 10, 80);
	}

	public Highway getHighway() {
		return highway;
	}

	public void setHighway(Highway highway) {
		this.highway = highway;
	}

	public String getViewMode() {
		return viewMode;
	}

	public void setViewMode(String viewMode) {
		this.viewMode = viewMode;
	}

}


